package work;

import work.getter.Dept;
import work.fram.JDBCConnect;
import work.fram.WriterMaker;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OrgCodeHandle {

    private static String ES = "\t";

    private static List<String> errors = new ArrayList<>();

    private static List<Map<String, String>> getNullOrg() {

        List<Map<String, String>> all = new ArrayList<>();

        Statement statement = JDBCConnect.getStatement();

        try {
            ResultSet resultSet = statement.executeQuery("SELECT name,idcard,sysid,remark FROM tc_md_personnel WHERE org_code IS NULL and create_time=\"2019-11-22 0:00:00\"");

            while (resultSet.next()) {
                String sysid = resultSet.getString("sysid");
                String remark = resultSet.getString("remark");
                String name = resultSet.getString("name");
                String idcard = resultSet.getString("idcard");

                if (name == null || idcard == null) {
                    System.out.println(sysid + ":" + "没有姓名或身份证号");
                    continue;
                }

                if (sysid == null) {
                    errors.add(name + ES + idcard + ES + "不存在sysid字段(疑似初始化数据)");
                    continue;
                }

                if (remark == null || remark.trim().equals("")) {
                    errors.add(name + ES + idcard  + ES + "不存在remark,无法处理");
                    continue;
                }

                Map<String, String> map = new HashMap<>();
                map.put("sysid", sysid);
                map.put("name", name);
                map.put("remark", remark);
                map.put("idcard", idcard);
                all.add(map);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return all;

    }


    private static Pattern pattern = Pattern.compile("([\\u4e00-\\u9fa5]+)[ \t\n]*([0-9\\-]+)");

    //使用正则进行处理
    private static String findOrgCodeFromRemark(String name, String idcard, String remark) {

        Matcher matcher = pattern.matcher(remark);

        //返回第二个
        if (matcher.find()) {
            return matcher.group(2);
        } else {
            errors.add(name + ES + idcard + ES + "无法从remark中提取组织机构编码(" + remark + ")");
        }
        return null;

    }


    //读取部门映射关系
    private static Map<String, String> dept ;

    static {
      //  dept = Dept.readDept();
    }

    private static String codeToName(String userName, String idcard, String remark, String code) {

        String name = dept.get(code.trim());

        if (name == null) {
            errors.add(userName + ES + idcard + ES + "从remark中提取的组织机构编码无法在部门表中找到对应记录(" + remark + "|" + code + ")");
        } else {
            System.out.println("非空");
        }
        return name;
    }


    public static void main(String[] args) {


        List<String> sqls = new ArrayList<>();

        String time = "2019-11-19 11:00:00";

        List<Map<String, String>> nullOrg = getNullOrg();

        for (Map<String, String> map : nullOrg) {

            String sysid = map.get("sysid");
            String remark = map.get("remark");
            String username = map.get("name");
            String idcard = map.get("idcard");

            String code = findOrgCodeFromRemark(username, idcard, remark);
            if (code != null) {
                String name = codeToName(username, idcard, remark, code);
                if (name != null) {
                    sqls.add("UPDATE tc_md_personnel SET create_time=\"" + time + "\", org_code=\"" + code + "\",org_name=\"" + name + "\" where sysid=\"" + sysid + "\";");
                }
            }

        }


        WriterMaker.MyWriter myWriter = WriterMaker.create("使用remark治理无部门编码(错误).txt");

        for (String error : errors) {
            myWriter.writeLine(error);
        }

        myWriter.close();

//        Statement statement = JDBCConnect.getStatement();

        myWriter = WriterMaker.create("使用remark治理无部门编码(正确).sql");

        for (String sql : sqls) {
            myWriter.writeLine(sql);
        }

        myWriter.close();
}


}
